<?php

class ImageComponent extends Object {

	function rotate($im, $rotation){
		$width = imagesx($im);
		$height = imagesy($im);
		if ( $rotation == '1'){
			//No Rotation, No Flip
			$im_rotated = $im;
		}
		if ( $rotation == '2'){
			//No Rotation, Flipped Horizontally
			$im_rotated = imagecreatetruecolor($width, $height);
			imagecopyresized($im_rotated, $im, 0, 0, ($width-1), 0, $width, $height, 0-$width, $height);
		}
		else if ( $rotation == '3'){
			//Rotated 180 degrees, No Flip
			$im_rotated = imagecreatetruecolor($width, $height);
			$im_rotated = imagerotate($im, 180, 0);
		}
		else if ( $rotation == '4'){
			//No Rotation, Flipped Vertically
			$im_rotated = imagecreatetruecolor($width, $height);
			imagecopyresized($im_rotated, $im, 0, 0, 0, ($height-1), $width, $height, $width, 0-$height);
		}
		else if ( $rotation == '5'){
			//Flipped Horizontally, Rotated 90 degrees counter clockwise
			$im_rotated = imagecreatetruecolor($height, $width);
			$im_temp = imagecreatetruecolor($width, $height);
			imagecopyresized($im_temp, $im, 0, 0, ($width-1), 0, $width, $height, 0-$width, $height);
			$im_rotated = imagerotate($im_temp, 90, 0);
			imagedestroy($im_temp);
		}
		else if ( $rotation == '6'){
			//No Flip, Rotated 90 degrees clockwise
			$im_rotated = imagecreatetruecolor($height, $width);
			$im_rotated = imagerotate($im, -90, 0);
		}
		else if ( $rotation == '7'){
			//Flipped Horizontally, Rotated 90 degrees clockwise
			$im_rotated = imagecreatetruecolor($height, $width);
			$im_temp = imagecreatetruecolor($width, $height);
			imagecopyresized($im_temp, $im, 0, 0, ($width-1), 0, $width, $height, 0-$width, $height);
			$im_rotated = imagerotate($im_temp, -90, 0);
			imagedestroy($im_temp);
		}
		else if ( $rotation == '8'){
			//No Flip, Rotated 90 degrees counter clockwise
			$im_rotated = imagecreatetruecolor($height, $width);
			$im_rotated = imagerotate($im, 90, 0);
		}
		return $im_rotated;
	}


	function resize($im, $max_height, $max_width) {
		$width = imagesx($im);
		$height = imagesy($im);
		if ($width > $height) {
			$new_width = $max_width;
			$new_height = $height * ($new_width / $width);
			$im_resized = imagecreatetruecolor($new_width, $new_height);
			imagecopyresampled($im_resized, $im, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		}
		else if ($height > $width) {
			$new_height = $max_height;
			$new_width = $width * ($new_height / $height);
			$im_resized = imagecreatetruecolor($new_width, $new_height);
			imagecopyresampled($im_resized, $im, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		}
		else {
			if ($max_height > $max_width) {
				$square_size = $max_width;
			}
			else if ($max_height < $max_width) {
				$square_size = $max_height;
			}
			else {
				$square_size = $max_height;
			}
			$new_height = $square_size;
			$new_width = $square_size;
			$im_resized = imagecreatetruecolor($new_width, $new_height);
			imagecopyresampled($im_resized, $im, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		}
		return $im_resized;
	}

	function thumbnail($im, $thumb_height, $thumb_width){
		$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
		$width = imagesx($im);
		$height = imagesy($im);
		imagecopyresampled($thumb, $im, 0, 0, ($width-$height)/2, 0, round($thumb_width * ($width/$height)), $thumb_height, $width, $height);
		return $thumb;
	}
	
	function create($filename, $type) {
		$ext = $this->getExtension($type);

		switch ($ext) :
			case 'gif' :
				$im = imagecreatefromgif($filename);
			break;

			case 'jpeg' :
				$im = imagecreatefromjpeg($filename);
			break;

			case 'png' :
				$im = imagecreatefrompng($filename);
			break;						
			
			default :
				$im = imagecreatefromjpeg($filename);			
			break;
		endswitch;
		
		return $im;
	} // create
	
	function getExtension($type) {
		$ext = explode('/', $type);
		$ext = $ext[1];
		
		return $ext;		
	} // getExtension

}

?>